﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HanGu.Services.Interface;
using HanGu.Entity;
using HanGu.Common;

namespace Hangu.WebClient.Controllers.Admin
{
    public class EveryDayWisdomController : AdminBaseController
    {
        private static readonly IEveryDayWisdom serviceWisdom = ServiceFactory.GetService<IEveryDayWisdom>();

        //
        // GET: /EveryDayWisdom/

        public ActionResult Index()
        {
            return View(viewList, GetEveryDayWisdomList());
        }

        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {

            SearchParma.Pagination.RowCount = null;
            return View(viewList, GetEveryDayWisdomList());
        }

        List<EveryDayWisdom> GetEveryDayWisdomList()
        {
            List<EveryDayWisdom> EveryDayWisdomList = serviceWisdom.GetAll(base.SearchParma);
            ViewData["RowCount"] = SearchParma.Pagination.RowCount;
            return EveryDayWisdomList;
        }

        public ActionResult Create(int id)
        {
            EveryDayWisdom EveryDayWisdom = new EveryDayWisdom() { GUID = Helper.GetGuid, CreateTime = DateTime.Now, Creator = CurentUser.UserName, UpdateTime = DateTime.Now, Updater = CurentUser.UserName };
            if (id > 0)
            {
                EveryDayWisdom = serviceWisdom.GetByID(id);
            }
            if (!EveryDayWisdom.IsIndex.HasValue)
            { EveryDayWisdom.IsIndex = false; }

            return View(viewCreate, EveryDayWisdom);
        }

        //
        // POST: /EveryDayWisdom/Create

        [HttpPost]
        [ValidateInput(false)]
        public string Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                EveryDayWisdom EveryDayWisdom = new EveryDayWisdom();
                TryUpdateModel(EveryDayWisdom, collection);
                EveryDayWisdom.IsIndex = collection["IsIndex"] != null;


                if (EveryDayWisdom.ID > 0)
                {
                    serviceWisdom.Update(EveryDayWisdom);
                }
                else
                {
                    serviceWisdom.Add(EveryDayWisdom);
                }
                
                ajaxStr = string.Format(ajaxStr, 200, ResourceManager.SaveSuccess, navTabId, "", "closeCurrent");

            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
            }
            return "{" + ajaxStr + "}";
        }

        //
        // GET: /EveryDayWisdom/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /EveryDayWisdom/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /EveryDayWisdom/Delete/5
        [HttpPost]
        public string Delete(int id, string guid)
        {
            try
            {
                //try
                //{
                //    FileManageController filemanage = new FileManageController();
                //    filemanage.DeleteExecute(guid);
                //}
                //catch { }
                serviceWisdom.Delete(id);
                ajaxStr = string.Format(ajaxStr, 200, ResourceManager.DeleteSuccess, navTabId, "", "");
            }
            catch (Exception ex)
            {
                ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
            }

            return "{" + ajaxStr + "}";

        }
    }
}
